/*
 * Created on Apr 4, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.prefs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.ui.MessageUtils;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class UploadActionForm extends Composite implements ConfigurationForm {
    private static final String OPTION_CUSTOM = ""; //$NON-NLS-1$
    private Button sortByExif;
    private Button removeUploaded;
    private Button enableAutoResize;
    private Button enableImageIOScaler;
    private Composite autoResizeGroup;
    private Configuration config = Configuration.getInstance();
    private Button option1280;
    private Button option1024;
    private Button option800;
    private Button optionCustom;
    private Text txtCustom;
    private Group radioGroup;
    private Label customLabel;

    public UploadActionForm(Composite parent) {
        super(parent, SWT.NONE);
        addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent arg0) {
                UploadActionForm.this.disposed();
            }
        });
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        setLayout(layout);
        sortByExif = new Button(this, SWT.CHECK);
        sortByExif.setText(Messages.getString("juploadr.ui.uploadactionform.label.sort.by.date")); //$NON-NLS-1$
        sortByExif.setSelection(config.getUploadByExifDate());
        removeUploaded = new Button(this, SWT.CHECK);
        removeUploaded.setText(Messages.getString("juploadr.ui.label.remove.uploaded")); //$NON-NLS-1$
        removeUploaded.setSelection(config.getAutoRemoveUploadedPhotos());

        layout = new GridLayout();
        layout.numColumns = 1;
        autoResizeGroup = new Composite(this, SWT.NONE);
        autoResizeGroup.setLayout(layout);
        autoResizeGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
        enableAutoResize = new Button(autoResizeGroup, SWT.CHECK);
        enableAutoResize.setText(Messages.getString("juploadr.ui.label.resize")); //$NON-NLS-1$
        enableAutoResize.setSelection(config.getAutoResizePhotos());

        enableAutoResize.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                UploadActionForm.this.toggleResizeControls();
            }
        });
        enableImageIOScaler = new Button(autoResizeGroup, SWT.CHECK);
        enableImageIOScaler.setText(Messages.getString("juploadr.ui.enable.high.quality.resizing"));
        enableImageIOScaler.setSelection(config.isScalingQualityFavored());
        enableImageIOScaler.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                UploadActionForm.this.toggleImageIo(enableImageIOScaler.getSelection());
            }

        });

        radioGroup = new Group(autoResizeGroup, SWT.SHADOW_NONE);
        layout = new GridLayout();
        layout.numColumns = 3;
        radioGroup.setText(Messages.getString("juploadr.ui.label.limit.maxsize")); //$NON-NLS-1$
        radioGroup.setLayout(layout);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

        radioGroup.setLayoutData(gridData);
        radioGroup.setEnabled(config.getAutoResizePhotos());

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 3;
        option1280 = getRadio(radioGroup, "1280"); //$NON-NLS-1$
        option1280.setLayoutData(gridData);

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 3;
        option1024 = getRadio(radioGroup, "1024"); //$NON-NLS-1$
        option1024.setLayoutData(gridData);

        gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = 3;
        option800 = getRadio(radioGroup, "800"); //$NON-NLS-1$
        option800.setLayoutData(gridData);
        optionCustom = getRadio(radioGroup, OPTION_CUSTOM);
        optionCustom.setText(OPTION_CUSTOM);
        optionCustom.setSelection(!(option1024.getSelection() || option800.getSelection() || option1280
                .getSelection()));
        txtCustom = new Text(radioGroup, SWT.SINGLE | SWT.BORDER);
        txtCustom.setEnabled(config.getAutoResizePhotos());
        if (optionCustom.getSelection()) {
            txtCustom.setText(String.valueOf(config.getMaxPhotoSize()));
        }
        GC gc = new GC(txtCustom);
        gridData = new GridData();
        gridData.widthHint = gc.textExtent("0000").x; //$NON-NLS-1$
        txtCustom.setLayoutData(gridData);
        txtCustom.addVerifyListener(new VerifyListener() {
            public void verifyText(VerifyEvent e) {
                if (e.text.length() > 0) {
                    try {
                        Integer.parseInt(e.text);
                    } catch (NumberFormatException e1) {
                        e.doit = false;
                    }
                }
            }
        });
        customLabel = new Label(radioGroup, SWT.NONE);
        customLabel.setText(Messages.getString("juploadr.ui.label.pixels.largest.side")); //$NON-NLS-1$
        customLabel.setEnabled(config.getAutoResizePhotos());
        gc.dispose();
    }

    protected void toggleImageIo(boolean enabled) {
        config.setImageIORescalerEnabled(enabled);
        if (enabled) {
            MessageUtils.showMessage(Messages
                    .getString("juploadr.ui.enable.high.quality.resizing.warning.message"), Messages
                    .getString("juploadr.ui.enable.high.quality.resizing.title"), SWT.ICON_WARNING);
        }
    }

    private Button getRadio(Group parent, String text) {
        int size = 0;
        Button b = new Button(parent, SWT.RADIO);
        try {
            size = Integer.parseInt(text);
            if (size == config.getMaxPhotoSize()) {
                b.setSelection(true);
            }
        } catch (NumberFormatException e) {

        }

        b.setText(text + Messages.getString("juploadr.ui.label.pixels.largest.side")); //$NON-NLS-1$
        b.setEnabled(config.getAutoResizePhotos());
        return b;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#saveChanges()
     */
    public void saveChanges() {
        config.setAutoRemoveUploadedPhotos(removeUploaded.getSelection());
        config.setAutoResizePhotos(enableAutoResize.getSelection());
        config.setUploadByExifDate(sortByExif.getSelection());
        if (config.getAutoResizePhotos()) {
            Control[] children = radioGroup.getChildren();
            for (int i = 0; i < children.length; i++) {
                if (children[i] instanceof Button) {
                    Button button = (Button) children[i];
                    if (button.getSelection()) {
                        if (OPTION_CUSTOM.equals(button.getText())) {
                            config.setMaxPhotoSize(Integer.parseInt(txtCustom.getText()));
                        } else {
                            String size = button.getText().substring(0, button.getText().indexOf(" ")); //$NON-NLS-1$
                            config.setMaxPhotoSize(Integer.parseInt(size));
                        }
                    }
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#validateChanges()
     */
    public Fault validateChanges() {
        Fault fault = null;
        if (optionCustom.getSelection() && txtCustom.getText().length() == 0) {
            fault = new Problem(Messages.getString("juploadr.ui.error.size.required")); //$NON-NLS-1$
        }
        return fault;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.prefs.ConfigurationForm#getFormName()
     */
    public String getFormName() {
        // TODO Auto-generated method stub
        return Messages.getString("juploadr.ui.uploadactionform.title"); //$NON-NLS-1$
    }

    public void toggleResizeControls() {
        boolean enabled = !radioGroup.isEnabled();
        radioGroup.setEnabled(enabled);
        Control[] radioButtions = radioGroup.getChildren();
        for (int i = 0; i < radioButtions.length; i++) {
            Control radio = radioButtions[i];
            radio.setEnabled(enabled);
        }
        this.redraw();
    }

    private void disposed() {
        removeUploaded.dispose();
        enableAutoResize.dispose();
        autoResizeGroup.dispose();
        option1024.dispose();
        option1280.dispose();
        option800.dispose();
        optionCustom.dispose();
        radioGroup.dispose();
    }

    public void setConfigurationDialog(ConfigurationDialog dialog) {
    }

    public void accountChanged() {
        // TODO Auto-generated method stub

    }
}